// packageH/free_of_charge/FreeDetails/FreeDetails.js
const App = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    orderId: null,
    goods: [],
    task: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (!options.order_id) {
      wx.showToast({
        title: "订单不存在",
        icon: "none",
      });
      setTimeout(() => {
        wx.navigateBack();
      }, 1000);
    }
    this.setData({
      orderId: options.order_id,
    });
    const eventChannel = this.getOpenerEventChannel();
    eventChannel.on("recordDetails", ({ data }) => {
      this.setData({
        task: data,
      });
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getOrderDetails();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  getOrderDetails() {
    wx.showLoading();
    let url = App.getNetAddresss(
      "plugin.free-lottery.frontend.index.order-info"
    );
    App._getNetWork({
      url,
      data: {
        order_id: this.data.orderId,
      },
      success: ({ data: { data, result, msg } }) => {
        wx.hideLoading();
        if (result === 0) {
          wx.showToast({
            title: msg,
            icon: "none",
          });
          return;
        }
        this.setData({
          goods: data,
        });
        wx.stopPullDownRefresh();
      },
      fail(err) {
        wx.hideLoading();
        wx.showToast({
          title: err.msg || err.message,
          icon: "none",
        });
      },
    });
  },
});
